package com.zhuhjay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.zhuhjay.mapper.BookMapper;
import com.zhuhjay.mapper.RecordMapper;
import com.zhuhjay.model.dto.RecordSearchDto;
import com.zhuhjay.model.entity.Book;
import com.zhuhjay.model.vo.RecordVo;
import com.zhuhjay.service.RecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZhuHJay
 * @date 2023/4/26 20:01
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Resource
    private BookMapper bookMapper;

    @Resource
    private RecordMapper recordMapper;

    @Override
    public List<RecordVo> search(RecordSearchDto searchDto) {
        return recordMapper.selectListByQuery(QueryWrapper.create()).stream().map(record -> {
            RecordVo vo = new RecordVo();
            BeanUtil.copyProperties(record, vo);
            Book book = bookMapper.selectOneById(record.getBookid());
            vo.setBookName(book.getName());
            vo.setPublish(book.getPublish());
            vo.setPrice(book.getPrice());
            vo.setAddress(book.getAddress());
            return vo;
        }).collect(Collectors.toList());
    }

}
